<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>LVS 的 nat 模式 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>LVS 的 nat 模式</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LVS/">LVS</a>
			</span>
		
	</div>

	

	
		<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>类似于iptables的DNAT，但支持多目标转发。客户端和realserver不能再同一个网段，不然直接响应，不走网关</strong></p>
<h2 id="1-模块讲解"><a href="#1-模块讲解" class="headerlink" title="1.模块讲解"></a>1.模块讲解</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端发起请求</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nat模式中，vip机器进行ip的转发，只改变目的ip</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;res1和res2需要提供web服务(nginx或者httpd都可以)</p>
<h2 id="2-请求流程图"><a href="#2-请求流程图" class="headerlink" title="2.请求流程图"></a>2.请求流程图</h2><ol>
<li>客户端发起请求到vip机器上，vip服务器根据lvs的算法，转发给realserver服务器（改变目的ip为res1或者res2），并记录连接信息，只改变目的ip，源ip不变。</li>
<li>real-server收到request请求包之后，发现目的ip是自己的ip，处理请求，然后走网关，经过vip</li>
<li>vip收到reply包后，修改reply包的源ip地址为vip，发给客户端</li>
<li>从客户端来的属于本地连接的包，查hash表，然后转发给real-server</li>
<li>当client发送完毕以后，此次连接结束或者连接超时，lvs自动从hast表中删除此条记录；</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据包流程如下：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.客户端发起请求：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">source</span>      	192.168.147.1			dest    192.168.147.150:80</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在vip服务器上，eth1网卡上抓包如下：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;2.lvs(vip)处理请求：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;vip服务器上抓包，在eth1网卡：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">source</span>		192.168.147.1:59334		dst	192.168.1.112/111:80</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;3.realserver处理完请求以后，网关是eth0；在vip的eth0上抓包如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">source</span>             192.168.1.112              dst    192.168.147.1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;4.包经过eth0返回vip服务器，然后经过eth1返回给客户端，源ip改成vip，目的ip不变，</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">source</span>		192.168.147.150           dst        192.168.147.1</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/04.jpeg?raw=true" alt=""></figure></p>
<h2 id="lvs-nat模式部署过程："><a href="#lvs-nat模式部署过程：" class="headerlink" title="lvs_nat模式部署过程："></a>lvs_nat模式部署过程：</h2><ol>
<li>（确保所有服务器的iptables   selinux都是关闭状态）</li>
<li>两个realserver上安装nginx或者http提供web服务</li>
<li>vip上安装ipvsadm </li>
<li>在vip服务器上新建lvs_nat.sh脚本，然后执行</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line"><span class="comment"># director 服务器上开启路由转发功能: </span></div><div class="line"><span class="built_in">echo</span> 1 &gt; /proc/sys/net/ipv4/ip_forward </div><div class="line"><span class="comment"># 关闭icmp的重定向</span></div><div class="line"><span class="built_in">echo</span> 0 &gt; /proc/sys/net/ipv4/conf/all/send_redirects</div><div class="line"><span class="built_in">echo</span> 0 &gt; /proc/sys/net/ipv4/conf/default/send_redirects</div><div class="line"><span class="built_in">echo</span> 0 &gt; /proc/sys/net/ipv4/conf/eth0/send_redirects</div><div class="line"><span class="built_in">echo</span> 0 &gt; /proc/sys/net/ipv4/conf/eth1/send_redirects</div><div class="line"></div><div class="line">iptables -F</div><div class="line">iptables -t nat -F</div><div class="line">iptables -t nat -X</div><div class="line"><span class="comment">#iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE</span></div><div class="line"></div><div class="line">ipvsadm -C</div><div class="line">ipvsadm -A -t 192.168.147.150:80 -s rr -p 300</div><div class="line"><span class="comment">#-p 300   长链接300秒</span></div><div class="line">ipvsadm -a -t 192.168.147.150:80 -r 192.168.1.111:80 -m -w 1</div><div class="line">ipvsadm -a -t 192.168.147.150:80 -r 192.168.1.112:80 -m -w 1</div></pre></td></tr></table></figure>
<ol>
<li>realserver的网关是eth0，及vip的内网ip</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>lvs——nat模式的缺点：当流量比较高时，因为所有的流量进入和出去都要进过lvs，所以瓶颈就在lvs上，而dr模式就比这个效果好很多</strong></p>
<h2 id="待解决问题"><a href="#待解决问题" class="headerlink" title="待解决问题"></a>待解决问题</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当访问res2的时候，在111上可以抓到112:80到147.1上的包，如下：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当访问res1时，在111上抓不到112:80到147.1上的包</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/LVS/2. LVS 的 nat 模式/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
